﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web;

namespace HPIT.Logistics.Web
{
    public class MyAspNetPager
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageSize">每页大小</param>
        /// <param name="currentPage">当前页🐎</param>
        /// <param name="totalCount">总的数据条数</param>
        /// <returns></returns>
        public static string ShowPage(int pageSize,int currentPage,int totalCount)
        {
            //原来的页面地址
            string redirectUrl = "";

            //默认显示10个页码
            int defaultPage = 10;

            //每页大小
            pageSize = pageSize == 0 ? 4 : pageSize;
           
            //总的页面
            int totalPages = (int)Math.Ceiling((float)totalCount/pageSize);

            int startPos = 1;
          

            //起始页码
            startPos = currentPage - defaultPage / 2;

            if (startPos < 1)
                startPos = 1;

            //分页内容
            var outStr = new StringBuilder();

            //大于1页才显示分页
            if(totalPages>1)
            {
                if(currentPage<=1)
                {
                    outStr.AppendFormat("<li class='page-item'><a class='page-link' href='#'>首页</a></li>");

                }
                else
                {
                    outStr.AppendFormat("<li class='page-item'><a class='page-link' href='{0}?page=1&pageSize={1}'>首页</a></li>", redirectUrl, pageSize);

                }

                if (currentPage>1)
                {
                    outStr.AppendFormat("<li class='page-item'><a class='page-link' href='{0}?page={1}&pageSize={2}'>上一页</a></li>", redirectUrl, currentPage-1,pageSize);

                }

               
                for(int i= startPos; i<= startPos+ defaultPage-1; i++)
                {

                    if (i > totalPages)
                        break;
                        if(currentPage==i)
                        {
                        outStr.AppendFormat("<li class='page-item active'><a class='page-link' href='{0}?page={1}&pageSize={2}'>{3}</a></li>",redirectUrl,i,pageSize,i);

                        }
                        else
                        {
                        outStr.AppendFormat("<li class='page-item'><a class='page-link' href='{0}?page={1}&pageSize={2}'>{3}</a></li>", redirectUrl,i, pageSize,i);

                        }
                     
                    
                }
                if(currentPage<totalPages)
                {
                    outStr.AppendFormat("<li class='page-item '><a class='page-link' href='{0}?page={1}&pageSize={2}'>下一页</a></li>", redirectUrl, currentPage+1, pageSize);

                }
            

              
                    
                outStr.AppendFormat("<li class='page-item '><a class='page-link' href='{0}?page={1}&pageSize={2}'>尾页</a></li>", redirectUrl,totalPages, pageSize);

                
            }
            return outStr.ToString();
        }
    }
}